<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<script type="text/javascript">

    $(function () {
        /**
         * 选择检查父项目
         */
        $('#parentExamType').change(function () {
            //清空执行科室
            $("#performedByName").val("");
            $("performedBy").val("");
            //清空子项目
            $("#examItemStr").html("");
            $("#clinicItemIds").val("");


            var parentId = $(this).children('option:selected').val();//父类检查项目id
            if(parentId=='' || parentId==null){
                return false;
            }
            //查询其子项目，动态拼接在子项目的select
            examSubSelect(parentId,'');
        });


        /**
        *保存
         */
        $("#btnSubmit").on("click",function(){
            var clinicItemIds=$("#clinicItemIds").val();
            if(clinicItemIds==null || clinicItemIds==""){
                toastr.error("请选择诊疗项目！");
            }else{
                $("#inputForm").submit();
                document.getElementById("inputForm").reset();
                $("#examItemStr").html("");
                $("#clinicItemIds").val("");
            }
          })
     })
    /**
    *   修改诊疗项目
     */
    function updateItem(){
        var parentId=$("#parentExamType").val();
        if(parentId==null || parentId==''){
            toastr.info("请选择检查项目类别后，在添加项目！");
            return false;
        }
        var clinicItemIds=$("#clinicItemIds").val();
        examSubSelect(parentId,clinicItemIds);
    }
    function examSubSelect(parentId,clinicItemIds){
        var dragHtml ='<div id="examDivId" style="overflow-y:auto;" class="drag-main">';		//检查项目
        dragHtml+='		<dl id="examSelected"  class="drag-selected"><dt>已选项目：</dt><br><dd class="drag-main-div"></dd></dl>';
        dragHtml+='</div>';
        $('#drag_h').html('<b>请选择检查项目</b><span onclick="exam.confirm()">确定</span>');
        $('#drag_con').html(dragHtml);
        exam.Show(parentId,clinicItemIds);
        boxAlpha();
        draglayer();
        //计算高度
        $("#examDivId").height($("#drag").height()-65);
    }
    var exam = {
        // 请选择地区
        init : function(clinicItemIds){
            if(clinicItemIds=='' || clinicItemIds==null){
                return false;
            }
            var strs= new Array(); //定义一数组
            strs=clinicItemIds.split(","); //字符分割
            for (var i=0;i<strs.length ;i++ )
            {
                exam.Chk(''+strs[i]+'');
            }
        },
        Show : function(parentId,clinicItemIds){
            $.ajax({
                type: "POST",
                url: '${ctx}/exam/examApply/queryByPId?parent.id=' + parentId,
                dataType: "json",
                success: function (data) {
                    if (data.length > 0) {
                        for(var i=0;i<data.length;i++){
                            var examHtml="";
                            examHtml+='<div id="maincity'+i+'">';
                            examHtml+='<h4>'+data[i].name+':</h4>';
                            examHtml+='<dl><dd class="drag-main-div">';
                            if(data[i].clinicItemDictList!=null){
                                for(var j=0;j<data[i].clinicItemDictList.length;j++) {
                                    examHtml+='<li class="selected" performedBy="'+data[i].clinicItemDictList[j].orgDept.id+'" deptName="'+data[i].clinicItemDictList[j].orgDept.deptName+'" examType="'+data[i].remarks+'"  price="'+data[i].clinicItemDictList[j].clinicItemDict.price+'" id="'+data[i].clinicItemDictList[j].clinicItemDict.id+'" onclick="exam.Chk(\''+data[i].clinicItemDictList[j].clinicItemDict.id+'\')">'+data[i].clinicItemDictList[j].clinicItemDict.itemName+'</li>';
                                }
                            }
                            examHtml+='</dd></dl>';
                            examHtml+='</div>';
                            $("#examSelected").after(examHtml);
                        }
                        if(clinicItemIds!="" && clinicItemIds!=null){
                            exam.init(clinicItemIds);
                        }
                    }
                }
            });
        },
        Chk: function(id){
            var isChk= $("#"+id).attr("class");
            if(isChk.indexOf("chkON")!=-1){
                $("#chk"+id).remove();
                $("#"+id).attr("class","selected");
                return false;
            }
            $("#"+id).addClass("chkON");
            var chkHtml=$("#"+id).clone(true);
            var liXm=$(chkHtml).attr("id","chk"+id);
            $("#examSelected dd").append(liXm);
            $("#chk"+id).attr("onclick","exam.del('chk"+id+"')");
            $("#chk"+id).attr("chkId",id);

        },
        del: function(id) {
            var chkId=$("#"+id).attr("chkId");
            $("#"+id).remove();
            $("#"+chkId).attr("class","selected");
        },
        confirm:function(){
            var itemIds="";
//            var deptId=$("#performedBy").val();
            var deptIds = "";
            var performedBy = "";
            $("#examItemStr").html("");
            $("#examSelected dd li").each(function(index,domEle){
                if(index == 0){
                    deptIds = $(this).attr("performedBy");
                }
                performedBy=$(this).attr("performedBy");
                //确认拿到执行科室
                if(deptIds==null || deptIds=="") {
                    toastr.error("获取执行科室失败！");
                    return;
                }else if(deptIds!=performedBy){
                    toastr.error("与已选择的诊疗项目不是同一执行科室！");
                    return;
                } else {
                    $("#performedBy").val(deptIds);
                    itemIds+=$(this).attr("chkId")+",";
                    var item=$(this).html()+"--￥："+'<font color="red">'+$(this).attr("price")+'</font>';
                    var deptName=$(this).attr("deptName");
                    var examType=$(this).attr("examType");
                    $("#performedByName").val(deptName);
                    $("#examType").val(examType);
                }
                $("#examItemStr").append(item+"<br>");
            });
            $("#clinicItemIds").val(itemIds.substring(0,itemIds.length-1));
            boxAlpha();
        }
    }
//    window.onload = function(){
//
//        $("#symptom").html(unescape($("#symptom").val()));
//
//        //escape
//    };

    function Trim(str,is_global){
        var result;
        result = str.replace(/(^\s+)|(\s+$)/g,"");
        if(is_global.toLowerCase()=="g")
        {
            result = result.replace(/\s/g,"");
        }
        return result;

    }

    $(function(){
        $("#symptom").val(unescape(Trim($("#symptom").val(),"g")));
    })
</script>
<div class="content-wrap no-p">
    <div class="wrapper">
        <%--填写数据--%>
        <div class="col-lg-11 mt10">
            <form:form id="inputForm" data-parsley-validate="" modelAttribute="examItem"
                       onsubmit="return formSaveLoad('rightDiv','inputForm','${ctx}/exam/examApply/save','${ctx}/exam/examApply/queryByCondition?clinicId=${examApply.clinicId}&patientId=${examApply.patientId}&inoroutFlag=0');"
                       method="post" class="form-horizontal">
                <form:hidden path="examApply.clinicId"/><%--就诊记录Id--%>
                <form:hidden path="examApply.patientId"/><%--病人Id--%>
                <form:hidden path="clinicItemIds"/><%--诊疗项目Ids已逗号分隔--%>
                <form:hidden path="itemName"/><%--子类检查项目名称--%>
                <form:hidden path="examApply.implementDept" id="performedBy"/><%--执行科室Id--%>
                <form:hidden path="examApply.inoroutFlag" value="0"/>
                <form:hidden path="examApply.examType" id="examType"/>
                <div class="form-group">
                    <label class="col-sm-3 control-label">检查类别：</label>
                    <div class="col-sm-3">
                        <form:select path="" id="parentExamType"  data-parsley-required="true" class="form-control" onchange="">
                            <form:option value="" label="请选择"></form:option>
                            <form:options items="${examTypeList}" itemLabel="name" itemValue="id" htmlEscape="false"/>
                        </form:select>
                    </div>
                    <label class="col-sm-3 control-label">执行科室：</label>
                    <div class="col-sm-3">
                        <form:input path="" id="performedByName" htmlEscape="false" class="form-control"
                                    disabled="true"/>
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-sm-3 control-label">诊疗项目：</label>
                    <div class="col-sm-9">
                        <div class="item_b" style="min-height: 60px" id="examItemStr" onclick="updateItem()"  data-parsley-required="true">

                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label">症状：</label>

                    <div class="col-sm-9">
                        <form:input path="examApply.symptom" id="symptom" value="${zhusu}" htmlEscape="false" class="form-control"/>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label">临床诊断：</label>

                    <div class="col-sm-9">
                        <textarea style="min-height: 90px;" htmlEscape="false" readonly="true" value="" class="form-control" disabled="true">${hisDiagnosisDes}</textarea>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label">其他诊断：</label>

                    <div class="col-sm-9">
                        <form:input path="examApply.relevantDiag" htmlEscape="false" class="form-control"
                                    maxlength="30"/>
                    </div>
                </div>


                <div class="form-group">
                    <label class="col-sm-3 control-label">体征：</label>

                    <div class="col-sm-9">
                        <form:textarea path="examApply.sign" htmlEscape="false" class="form-control" maxlength="500"/>
                    </div>
                </div>

                <%--<div class="form-group">--%>
                    <%--<label class="col-sm-3 control-label">化验结果：</label>--%>

                    <%--<div class="col-sm-9">--%>
                        <%--<form:textarea path="examApply.relevantResult" htmlEscape="false" class="form-control"--%>
                                       <%--maxlength="500"/>--%>
                    <%--</div>--%>
                <%--</div>--%>
                <div class="form-group">
                    <label class="col-sm-3 control-label"></label>
                    <div class="col-sm-offset-2 col-sm-9">
                        <input id="btnSubmit" class="btn btn-primary" type="button" value="保 存">&nbsp;
                    </div>
                </div>
            </form:form>
        </div>

    </div>
</div>
